package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.CommentService;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * Comment 已知 :
     *      publishId :动态id
     *      userId ; 操作人
     *      commentType : //操作类型： 1-对动态点赞，2-对动态评论，3-对动态喜欢  4-对视频点赞   5-对视频评论    6-对评论点赞
     * @param comment
     * @return
     */
    @Override
    public Integer saveMovementComment(Comment comment) {
        //1.根据动态id查询动态详情
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);


        //2.保存comment
        comment.setCreated(System.currentTimeMillis());
        comment.setPublishUserId( movement.getUserId() ); //赋值发动态的人id
        mongoTemplate.save(comment);

        //3.考虑的是 点赞数 评论数  喜欢数据 +1
        if(comment.getCommentType() == 1 ){
            movement.setLikeCount( movement.getLikeCount() + 1 );
        }else if(comment.getCommentType() == 2 ){
            movement.setCommentCount( movement.getCommentCount() + 1 );
        }else if(comment.getCommentType() == 3){
            movement.setLoveCount( movement.getLoveCount() + 1 );
        }
        //更新一下详情表
        mongoTemplate.save(movement);// 如果数据库已经存在 执行修改动作 不存在就是新增

        //4.返回 次数

        if(comment.getCommentType() == 1 ){
            return movement.getLikeCount();
        }else if(comment.getCommentType() == 2 ){
            return movement.getCommentCount();
        }else if(comment.getCommentType() == 3){
            return movement.getLoveCount();
        }
        return 0;
    }

    /**
     * 删除评论表
     * @param comment
     * @return
     */
    @Override
    public Integer deleteMovementComment(Comment comment) {
        //1.根据动态id查询动态详情
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);


        //2.删除comment  动态id 只是定位到某一组操作
        //根据 动态id 用户的id 和操作类型 锁定到某一条数据进行删除
        Query query = new Query(
                new Criteria().where("publishId").is(comment.getPublishId())
                .and("userId").is(comment.getUserId())
                .and("commentType").is(comment.getCommentType())
        );
        mongoTemplate.remove(query , Comment.class);

        //3.考虑的是 点赞数 评论数  喜欢数据 +1
        if(comment.getCommentType() == 1 ){
            movement.setLikeCount( movement.getLikeCount() - 1 );
        }else if(comment.getCommentType() == 2 ){
            movement.setCommentCount( movement.getCommentCount() - 1 );
        }else if(comment.getCommentType() == 3){
            movement.setLoveCount( movement.getLoveCount()- 1 );
        }
        //更新一下详情表
        mongoTemplate.save(movement);// 如果数据库已经存在 执行修改动作 不存在就是新增

        //4.返回 次数

        if(comment.getCommentType() == 1 ){
            return movement.getLikeCount();
        }else if(comment.getCommentType() == 2 ){
            return movement.getCommentCount();
        }else if(comment.getCommentType() == 3){
            return movement.getLoveCount();
        }
        return 0;
    }

    /**
     * 分页查询 动态下的评论
     * @param movementId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageBeanVo findMovementComment(String movementId, Integer pageNum, Integer pageSize) {

        //pageNum = 1 ;
        int startIndex = (pageNum - 1)*pageSize;
        //分页查询数据
        Query query = new Query(
                new Criteria().where("publishId").is( new ObjectId(movementId) )
                .and("commentType").is(2)
        )
                .with(Sort.by(Sort.Order.desc("created")))
                .skip(startIndex).limit(pageSize);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        //查询总记录数
        long counts = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(pageNum , pageSize , counts , comments);
    }

    /**
     * 根据用户的id 和 操作的类型查询评论列表的数据
     * @param pageNumber
     * @param pageSize
     * @param commentType
     * @return
     */
    @Override
    public PageBeanVo findCommentByCommentType(Integer pageNumber, Integer pageSize, int commentType, Long userId) {
        int startIndex = (pageNumber-1)*pageSize;
        Query query = new Query(
                new Criteria().where("publishUserId").is(userId) //此处传入的是当前登录的用户id ,需要查询的是 发布动态人的id相等即可
                .and("commentType").is(commentType)
        )
                .with(Sort.by(Sort.Order.desc("created")))
                .skip(startIndex).limit(pageSize);

        //查询记录
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        //查询总记录数据
        long counts = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(pageNumber , pageSize , counts , commentList);
    }
}
